/*
 * @Author: cola(sunshiwen)
 * @Date: 2022-04-19 09:11:44
 * @LastEditTime: 2022-08-03 22:25:25
 * @LastEditors: cola(sunshiwen)
 * @Description: Sequelize config
 * @FilePath: /maycur-koa/common/mysql.js
 */
'use strict';

const Sequelize = require('sequelize');

const { mysql: { database, username, password, host, pool } } = require('../config');
const log = require('./log');

let _mysqlExtraConf = {
    host,
    dialect: 'mysql',
    pool,
    timezone: '+08:00',
   // logging: (...values) => log.debug(...values),
}
// 同步下不能使用 logging
if (process.env.DB_SYNC) {
    delete _mysqlExtraConf.logging;
}

const mysqlServer = new Sequelize(database, username, password, _mysqlExtraConf);

// 测试连接
mysqlServer
    .authenticate()
    .then(() => {
        log.info(`Mysql(${host}) Connection has been established successfully.`);
    })
    .catch(err => {
        log.error('Unable to connect to the database:', err);
    });


module.exports = mysqlServer;